package cate.game.role.pay.trademall.monthcard;

import cate.common.table.d.GDFunc;
import cate.common.table.d.GDOperation;
import cate.common.table.trademall.monthcard.MonthCardRow;
import cate.common.util.GameResult;
import cate.common.util.TimeTool;
import cate.game.chat.ChatContentParam;
import cate.game.mail.po.MailBuilder;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.RoleDependent;
import cate.game.role.mail.Mail;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.ArrayList;
import java.util.List;

@NoteClass(value = "月卡档位数据")
public class MonthCardStatusPO extends RoleDependent {
    @NoteField(value = "模板ID")
    public int tid;
    @NoteField(value = "是否激活")
    public boolean active;
    //    @NoteField(value = "月卡档位结束时间")
//    public long endTime;
    @NoteField(value = "上次领取时间")
    public long lastGetTime;
    //    @NoteField(value = "激活奖励次数")
//    public int activeRewardCount;
    @NoteField(value = "领取次数")
    public int receiveNum;
    @NoteField(value = "累计充值数量")
    public long payRecord;

    public MonthCardStatusPO() {
    }

    public MonthCardStatusPO(int tid) {
        this.tid = tid;
    }

    @Override
    public void onDaySpan(boolean silence) {
        if (!active) {
            return;
        }
//        checkMailSupply();
//        checkInactive();
        if (receiveNum >= 30) {
            active = false;
        }
    }

    /**
     * 获取配置
     */
    @JsonIgnore
    public MonthCardRow getConfigRow() {
        return role.getGame().table.tradeMall.month_card.get(tid);
    }

    /**
     * 检测是否过期
     */
    private void checkInactive() {
        if (!active) {
            return;
        }
        if (receiveNum >= 30) {
            active = false;
            role.getRes().expireNotice.addFunc(GDFunc.TRADE_MALL_MONTH_CARD);
        }
    }

    /**
     * 是否允许激活
     *
     * @param payItemId 充值项ID
     * @return 检测结果 无携带数据
     */
    public GameResult<Void> allowActive(int payItemId) {
        GameResult<Void> r = new GameResult<>();
        MonthCardRow configRow = getConfigRow();
        if (configRow == null) {
            return r.fail("配置不存在");
        }
        /*if (payItemId == configRow.payItem) {
            if (active) {
                return r.fail("当前已激活");
            }
        } else if (payItemId == configRow.discountPayItem) {
            if (configRow.discountDay <= 0) {
                return r.fail("当前不支持折扣充值");
            }
            if (!active) {
                return r.fail("不在折扣期间");
            }
//            if (TimeTool.getDistanceDays(System.currentTimeMillis(), endTime) > configRow.discountDay) {
//                return r.fail("不在折扣期间");
//            }
        } else {
            return r.fail("充值项不匹配");
        }*/
        return r.success();
    }

    /**
     * 月卡付费激活
     */
    public void active() {
        checkInactive();
        MonthCardRow configRow = getConfigRow();
        if (configRow == null) {
            return;
        }
        long nowDay = TimeTool.getZeroTimeStamp(game().time.now());
        // 默认上次领取时间为前一天 便于计算隔天补发
        lastGetTime = nowDay - TimeTool.TimeCons.DAY;
        active = true;
        if (configRow.lampId != 0) {
            List<ChatContentParam> params = new ArrayList<>();
            params.add(ChatContentParam.c(role.getBase().name));
            role.getGame().notice.lampOnline(role, configRow.lampId, params);
        }
        payRecord = 0;
        receiveNum = 0;
        //激活后不发奖励，手动领取
        //getReward(true);
    }

    /**
     * 检测月卡补发
     */
    public void checkMailSupply() {
        MonthCardRow configRow = getConfigRow();
        if (configRow == null) {
            return;
        }
        long lastDay = TimeTool.getZeroTimeStamp(lastGetTime);
        long nowDay = TimeTool.getZeroTimeStamp(game().time.now());
//        long supplyNum = (Math.min(nowDay, endTime) - lastDay) / TimeTool.TimeCons.DAY - 1;
//        if (supplyNum <= 0) {
//            return;
//        }
        // 根据数量补发
        MixRes mixRes = new MixRes(configRow.dailyRewardStr);
        if (mixRes.isEmpty()) {
            return;
        }
//        mixRes.items.forEach(item -> item.num *= supplyNum);
        Mail supplyMail = new MailBuilder()
                .setContentId(configRow.mailId)
                .setReward(mixRes)
                .build();
        role.getGame().mail.send(role, supplyMail);
        // 上次领奖天数设为前一天
        lastGetTime = nowDay - TimeTool.TimeCons.DAY;
    }

    /**
     * 领取月卡奖励
     *
     * @return 领取结果 无携带数据
     */
    @JsonIgnore
    public EcResult<Void> getReward(boolean sendMail) {
        EcResult<Void> r = new EcResult<>();
        if (!active) {
            return r.fail("未激活月卡");
        }
        MonthCardRow configRow = getConfigRow();
        if (configRow == null) {
            return r.fail("找不到配置");
        }
        long nowTime = game().time.now();;
        if (TimeTool.getDistanceDays(lastGetTime, nowTime) <= 0) {
            return r.fail("今日已领取");
        }
        //领取持续奖励
        MixRes mixRes = new MixRes(configRow.dailyRewardStr);
        if (mixRes.isEmpty()) {
            return r.fail("每日奖励为空");
        }
        EcResult<?> r_res = new MixResAdder().setRes(mixRes)
                .setOperation(GDOperation.TRADE_MALL_MONTH_CARD)
                .setOperationSub(tid)
                .setDialogReward(true)
                .exe(role);
        if (!r_res.ok()) {
            return r.fail(r_res.message);
        }
        role.getHistory().action.MonthCardGet(tid);
        // 标记领奖时间
        lastGetTime = nowTime;
        receiveNum++;
        if (receiveNum >= 30) {
            active = false;
        }
        return r.success();
    }

    /**
     * 充值触发
     *
     * @param payNum 充值数量
     */
    public void onPay(long payNum) {
        if (active) {
            return;
        }
        MonthCardRow configRow = getConfigRow();
        if (configRow == null) {
            return;
        }
        payRecord += payNum;
        if (payRecord >= configRow.payTarget * 100) {
            active();
        }
        role.getPay().tradeMall.monthCard.noticeUpdate();
    }
}
